package cn.ctodb.cms.service;

import cn.ctodb.cms.domain.CmsFile;
import cn.ctodb.cms.repository.CmsFileRepository;
import cn.ctodb.cms.repository.search.CmsFileSearchRepository;
import cn.ctodb.cms.service.dto.CmsFileDTO;
import cn.ctodb.cms.service.mapper.CmsFileMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing CmsFile.
 */
@Service
@Transactional
public class CmsFileService {

    private final Logger log = LoggerFactory.getLogger(CmsFileService.class);
    
    @Inject
    private CmsFileRepository cmsFileRepository;

    @Inject
    private CmsFileMapper cmsFileMapper;

    @Inject
    private CmsFileSearchRepository cmsFileSearchRepository;

    /**
     * Save a cmsFile.
     *
     * @param cmsFileDTO the entity to save
     * @return the persisted entity
     */
    public CmsFileDTO save(CmsFileDTO cmsFileDTO) {
        log.debug("Request to save CmsFile : {}", cmsFileDTO);
        CmsFile cmsFile = cmsFileMapper.cmsFileDTOToCmsFile(cmsFileDTO);
        cmsFile = cmsFileRepository.save(cmsFile);
        CmsFileDTO result = cmsFileMapper.cmsFileToCmsFileDTO(cmsFile);
        cmsFileSearchRepository.save(cmsFile);
        return result;
    }

    /**
     *  Get all the cmsFiles.
     *  
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public List<CmsFileDTO> findAll() {
        log.debug("Request to get all CmsFiles");
        List<CmsFileDTO> result = cmsFileRepository.findAll().stream()
            .map(cmsFileMapper::cmsFileToCmsFileDTO)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     *  Get one cmsFile by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public CmsFileDTO findOne(Long id) {
        log.debug("Request to get CmsFile : {}", id);
        CmsFile cmsFile = cmsFileRepository.findOne(id);
        CmsFileDTO cmsFileDTO = cmsFileMapper.cmsFileToCmsFileDTO(cmsFile);
        return cmsFileDTO;
    }

    /**
     *  Delete the  cmsFile by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete CmsFile : {}", id);
        cmsFileRepository.delete(id);
        cmsFileSearchRepository.delete(id);
    }

    /**
     * Search for the cmsFile corresponding to the query.
     *
     *  @param query the query of the search
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<CmsFileDTO> search(String query) {
        log.debug("Request to search CmsFiles for query {}", query);
        return StreamSupport
            .stream(cmsFileSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .map(cmsFileMapper::cmsFileToCmsFileDTO)
            .collect(Collectors.toList());
    }
}
